package com.symaster.common.entity;

import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;

/**
 * @author yinmiao
 * @version 2022-07-01 14:53
 */
public class BasePageVo {
    private Integer page;
    private Integer pageSize;

    public PageRequest ofPageRequest(Sort sort) {
        Page page = new Page(getPage(), getPageSize());
        return PageRequest.of(page.getPage() - 1, page.getPageSize(), sort);
    }

    public PageRequest ofPageRequest(Sort.Direction direction, String... properties) {
        Page page = new Page(getPage(), getPageSize());
        return PageRequest.of(page.getPage() - 1, page.getPageSize(), direction, properties);
    }

    public PageRequest ofPageRequest() {
        Page page = new Page(getPage(), getPageSize());
        return PageRequest.of(page.getPage() - 1, page.getPageSize());
    }

    public int[] toLimit() {
        int page = getPage() == null ? 1 : getPage();
        int pageSize = getPageSize() == null ? 30 : getPageSize();
        return new int[]{(page - 1) * pageSize, pageSize};
    }

    public Integer getPage() {
        return page;
    }

    public void setPage(Integer page) {
        this.page = page;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    @Override
    public String toString() {
        return "BasePageVo{" +
                "page=" + page +
                ", pageSize=" + pageSize +
                '}';
    }
}
